<?php
/*
客户管理
*/
//define('Acache_no','1');
class Con_qq extends Acan{
	public $defaultAct='my';
	public $Conread = 'QQ管理';
	public $Actread = array('list'=>'所有QQ','my'=>'我的QQ','save'=>'保存','del'=>'删除','delall'=>'删除所有');
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		if(empty($this->id) || $this->id == '*'){$this->id = '0';}
		$this->uid = Afun_uid();
		$this->model = Afun_db()->model(Acan_table_crm_qq);
		//print_r($this->model);
	}
	//--所有QQ
	function Act_list(){
		$this->atitle = '所有QQ';
		return $this->Act_my();
	}
	//--我的QQ
	function Act_my(){
		if(empty($this->atitle)){$this->atitle = '我的QQ';}
		$uid = $this->uid;
		if($this->id>1){
			$uid = $this->id;
		}
		$obj = Afun_acan_new('list');
		$key_name = $this->model->key_name;
		$columnrr = $this->model->comment;//初始栏目
		$columnrr = Afun_arr_remove($this->model->comment,array(''));
		$this->model->comment['username'] = '所属用户';
		$column = $obj->column($this->model,$columnrr,$_POST['acolumn']);
		$column_html = $obj->column_html($this->model,$column,'Aform');
		$column_json = $obj->form_json($this->model);
		$column_sop = $this->model->comment;
		//分页函数
		if($this->Aact=='list'){
			$con->where = "1".$where;
		}else{
			$con->where = "a.uid=$uid".$where;
		}
		$con->select = "a.*,b.username";
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $this->model->key_name;
		$page->page = '10';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.$pages->orderby";
		$con->join = "LEFT JOIN `".Acan_table_user."` b ON a.uid=b.uid";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->model->ofindAll($con);
		if(is_array($list))
		foreach($list as $k=>$v){
			$listjson[$v->$key_name] = $v;
		}
		$listjson = Ajson_encode($listjson);
		include $this->Atpl($this->Amod.'/my');
	}
	//--获取单用户信息
	function Act_info(){
		if($this->id<1){
			return;
		}
		$con->select = "a.*,b.username";
		$con->join = "LEFT JOIN `".Acan_table_user."` b ON a.uid=b.uid";
		$con->where = "a.cid=".$this->id;
		$info = $this->model->ofind($con);
		Afun_json($info);
	}
	//批量更新
	function Act_save(){
		$savedata = $_POST['save'];
		$savedata['uptime'] = Afun_get_time();
		if($savedata['sign_status']>0){
			$savedata['sign_time'] = Afun_get_time();
		}
		if($this->id == 'one'){
			$key = $savedata[$this->model->key_name];
			unset($savedata[$this->model->key_name]);
			if(!empty($key)){
				$msg = $this->model->updatePk($savedata,$key);
			}else{//--添加一个新用户
				$savedata['uid'] = $this->uid;
				$msg = $this->model->save($savedata);
			}
		}elseif(is_array($savedata)){
			foreach($savedata as $i=>$vals){
				$this->model->updatePk($vals,$vals['key']);
			}
		}
		Afun_acan_new('list')->delcache(Acan_table_crm_customer);
		Afun_json($msg);
	}
	//删除
	function Act_del(){
		$data = $this->model->findPk($this->id);
		if($this->model->deletePk($this->id)){
			Afunc_group()->usergroupdel($this->id);
			echo '删除成功';
		}
		Afun_acan_new('list')->delcache(Acan_table_crm_customer);
	}
	//删除所选
	function Act_delall(){
		if(!is_array($_POST['chk'])){Afun_refresh("请先选择",'referrer');}
		foreach($_POST['chk'] as $key=>$val){
			Afunc_group()->usergroupdel($val);
			if(empty($vala))
				$vala = "'$val'";
			else
				$vala .=",'$val'";
		}
		$this->model->delete($this->model->key_name." IN ($vala)".$where);
		Afun_acan_new('list')->delcache(Acan_table_crm_customer);
		Afun_refresh("删除成功",'referrer');
	}
}

?>